<?php

namespace App\Http\Controllers\Admin\Site;

use App\B2BProject\Admin\AdminControllerBuildTrait;
use App\B2BProject\Site\Article;
use App\B2BProject\Site\ArticleCategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    use AdminControllerBuildTrait;

    protected $moduleName = '文章管理';

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $articles = Article::paginate(15);

        return view('admin/site/article/index')->with('articles', $articles);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categories = ArticleCategory::where('parent_id', 0)->get();

        return view('admin/site/article/create')->with('categories', $categories);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = \Validator::make($request->all(), [
            'title' => 'required|min:4',
            'href' => 'url',
            'category_id' => 'required|numeric|exists:article_categories,id',
            'sort' => 'required|numeric',
            'display' => 'required|boolean',
            'content' => 'required'
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        $article = Article::create($request->only(['title', 'href', 'display', 'sort', 'content']));

        $content = $request->input('content');

        if (preg_match_all("/<img\\s+src=\"(.*?)\".*?\\/\\>/ui", $content, $matches)) {
            $first = $matches[1];
            $article->cover = $first[0];
        }

        ArticleCategory::findOrFail($request->input('category_id'))->articles()->save($article);
        \Admin::user()->articles()->save($article);

        return redirect()->route('admin.article.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $article = Article::findOrFail($id);

        $categories = ArticleCategory::where('parent_id', 0)->get();

        return view('admin/site/article/edit')->with('target', $article)->with('categories', $categories);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = \Validator::make($request->all(), [
            'title' => 'required|min:4',
            'href' => 'url',
            'category_id' => 'required|numeric|exists:article_categories,id',
            'sort' => 'required|numeric',
            'display' => 'required|boolean',
            'content' => 'required'
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }



        $article = Article::findOrFail($id);
        $article->update($request->only(['title', 'href', 'sort', 'display', 'content']));

        // TODO 从文章中取出 图片地址
        $content = $request->input('content');

        if (preg_match_all("/<img\\s+src=\"(.*?)\".*?\\/\\>/ui", $content, $matches)) {
            $first = $matches[1];
            $article->cover = $first[0];
            $article->save();
        }

        ArticleCategory::findOrFail($request->input('category_id'))->articles()->save($article);

        return redirect()->back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Article::findOrFail($id)->delete();

        return redirect()->back();
    }
}
